package com.lt.flashcard.view;

import java.util.ArrayList;
import java.util.List;

import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.InputEvent;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.FCSComment;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.Notification;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.FlashcardSetService;
import com.lt.flashcard.model.service.NotificationService;
import com.lt.flashcard.model.service.Repository;

public class V_fcscommentVM {
	private int nuChars = 500;
	private String fcsCommentParentId = null;
	private List<FCSComment> fcsCommentList;
	private Long pagination = (long) 1;
	private Long fcsId;
	
	@Wire
	Textbox comment;

	@Init
	public void Init(){
    	FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
    	fcsId = new Long(Executions.getCurrent().getParameter("id"));
    	getFcsCommentListLimit();
    	pagination = srv.countPage(fcsId);
	}
	@AfterCompose
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
    	Selectors.wireComponents(view, this, false);
    }
	
	//--->setters and getters
	public List<FCSComment> getFcsCommentList() {
		if(fcsCommentList == null) {
            fcsCommentList = new ArrayList<FCSComment>();
        }
    	return fcsCommentList;
    }
	
	public void setFcsCommentList(List<FCSComment> fcsCommentList) {
    	this.fcsCommentList = fcsCommentList;
    }
	
	public int getNuChars(){
		return nuChars;
	}
	
	private User getUser(){
		return Utility.getUser();
	}
	
	private Long getPagination(){
		return pagination;
	}
	//<-- end setters and getters
	
	
	private List<FCSComment> getFcsCommentListLimit(){
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		String nuPage = Executions.getCurrent().getParameter("page");
		int page = 1;
		if(nuPage != null) {
            page = Integer.parseInt(nuPage);
        }
    	fcsCommentList = srv.getCommentsOfFlashcardset(fcsId, page);
    	return fcsCommentList;
	}
	private FCSComment FCSComment(){
		User user = getUser();
		FCSComment fcscomment = new FCSComment();
		fcscomment.setFcsid(fcsId);
		fcscomment.setMessage(comment.getValue());
		fcscomment.setUid(user.getId());
		fcscomment.setUsername(user.getUsername());
		return fcscomment;
	}
	private void saveNotification(String type){
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		NotificationService nsrv = Repository.getInstance().getNotificationService();
		FlashcardSet flashcardset = srv.findByID(fcsId);
		User user = Utility.getUser();
		User author = flashcardset.getAuthor();
		if(!user.equals(author)){
    		Notification notify = new Notification();
    		notify.setSourceuid(user.getId());
    		notify.setSourceuname(user.getUsername());
    		notify.setTargetuid(author.getId());
    		notify.setRelatedid(fcsId);
    		notify.setRelatedname(flashcardset.getName());
    		if(type.equals("reply")) {
                notify.setType(Notification.TYPE_REPLYCOMMENT);
            } else {
                notify.setType(Notification.TYPE_COMMENT);
            }
    		nsrv.saveNotification(notify);
		}
	}
	@Command
	@NotifyChange("nuChars")
	public void countChars(@ContextParam(ContextType.TRIGGER_EVENT)InputEvent event){
		nuChars = 500 - event.getValue().length();
	}
	@Command
	@NotifyChange("fcsCommentList")
	public void submitComment(){
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		
		FCSComment fcscomment = FCSComment();
		if(fcsCommentParentId != null){//save reply
    		FCSComment fcscommentparent = srv.loadFCSComment(fcsCommentParentId);
    		if(fcscommentparent!=null){
        		fcscommentparent.getReplies().add(fcscomment);
        		srv.saveFCSComment(fcscommentparent);
        		fcsCommentParentId = null;
        		
        		saveNotification("reply");
    		}else{
    			Messagebox.show("khong tim thay comment parent");
    		}
		}else{//save comment
			srv.saveFCSComment(fcscomment);
			saveNotification("comment");
		}
		
		comment.setValue("");
		fcsCommentList = getFcsCommentListLimit();
		
		Clients.evalJavaScript(" $('.list_comment li').hover(function(){" +
							  	"$(this).css({'background':'#fff','box-shadow':'3px 3px 3px #BCBCBC'});" +
							  	"});" +
							  	" $('.list_comment li').hover(" +
							  		"function () {" +
							  			"$('.action_list_comment', this).show();" +
							  		"}," +
							  		"function () {" +
							  			"$('.action_list_comment', this).hide(); " +
							  			"$(this).css({'background':'none','box-shadow':'none'});  " +
						  		"});");
	}
	
	@Command
	public void doReplyComment(@BindingParam("fcsCommentParent") FCSComment fcsCommentParent){
		this.fcsCommentParentId = fcsCommentParent.getId().toString();
		comment.focus();
	}
	
	@Command
	@NotifyChange("fcsCommentList")
	public void doDeleteComment(@BindingParam("fcsComment") FCSComment fcsComment, @BindingParam("parent") Long parent){
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		if(fcsComment.getId() != null) {
            srv.deleteFCSComment(fcsComment, srv.findByID(fcsId));
        } else{
			Messagebox.show(parent.toString());
		}
		fcsCommentList = getFcsCommentListLimit();
		
		Clients.evalJavaScript(
			" $('.list_comment li').hover(function(){" +
			  	"$(this).css({'background':'#fff','box-shadow':'3px 3px 3px #BCBCBC'});" +
		  	"});" +
		  	" $('.list_comment li').hover(" +
		  		"function () {" +
		  			"$('.action_list_comment', this).show();" +
		  		"}," +
		  		"function () {" +
		  			"$('.action_list_comment', this).hide(); " +
		  			"$(this).css({'background':'none','box-shadow':'none'});  " +
	  		"});"
		);
	}
}